package com.dao.impl;

import com.Bean.RoomType;
import com.dao.RoomTypeDao;
import com.util.JdbcUtil;
import com.util.PageUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomTypeDaoImpl implements RoomTypeDao {
    @Override
    public int insert(RoomType roomType) throws SQLException {
        double a = 0;
        double b = 0;
        String[] s1 = roomType.getPrice().split(",");
        String[] s2 = roomType.getRid().split(",");
        a = Double.parseDouble(s1[0]);
        b = Double.parseDouble(s1[1]);
        System.out.println("ssss"+a+"  "+b+""+roomType);
        String sql = "INSERT INTO room(rtid,rname,wkprice,weprice,rnumber) VALUES(?,?,?,?,?)";
        int res = JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                roomType.getRtid(),roomType.getRname(),a,b,roomType.getRnumber()
        });
        for (int i = 0; i < s2.length; i++) {
            String sql1 = "INSERT INTO room_id(rtid,rid,rstatus,timetype) VALUES(?,?,?,?)";

            JdbcUtil.insertOrUpdateOrDelete(sql1,new Object[]{
                    roomType.getRtid(),s2[i],"空房",1
            });
        }
        return  res;
    }

    @Override
    public int update(RoomType roomType) throws SQLException {
        double a = 0;
        double b = 0;
        String[] s1 = roomType.getPrice().split(",");
        String[] s2 = roomType.getRid().split(",");
        a = Double.parseDouble(s1[0]);
        b = Double.parseDouble(s1[1]);

        String sql = "UPDATE `room` SET rname=?,weprice=?,weprice=?,rnumber=? WHERE rtid=?";

        int res = JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                roomType.getRname(),a,b,roomType.getRnumber(),roomType.getRtid()
        });

        for (int i = 0; i < s2.length; i++) {
            String sql1 = "UPDATE `room_id` SET rid=? WHERE rtid=?";

            JdbcUtil.insertOrUpdateOrDelete(sql1,new Object[]{
                    roomType.getRtid(),s2[i]
            });
        }

        return 0;
    }

    @Override
    public List<RoomType> queryData(RoomType roomType) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "SELECT r.rtid,r.rname,CONCAT(r.wkprice,',',r.weprice) price,r.rnumber,GROUP_CONCAT(i.rid) rid FROM room r JOIN room_id i WHERE r.rtid=i.rtid GROUP BY rtid ";

        return JdbcUtil.queryData(sql, RoomType.class);
    }



    @Override
    public int deleteById(Object id) throws SQLException {
        Boolean t = true;
        String[] i = new String[]{"1","2","3","4","5"};
        for (int j = 0; j < i.length; j++) {
            if (id.equals(i[j])){
                t=false;
            }
        }
        String sql1;
        String sql2;
        if (t){
            sql1 = "DELETE FROM room WHERE rtid=?";
            sql2 = "DELETE FROM room_id WHERE rtid=?";
            JdbcUtil.insertOrUpdateOrDelete(sql2, new Object[]{id});
            JdbcUtil.insertOrUpdateOrDelete(sql1, new Object[]{id});
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public PageUtil<RoomType> queryDataBypage(int currentPgae, int pageSize, RoomType roomType) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "SELECT r.rtid,r.rname,CONCAT(r.wkprice,',',r.weprice) price,r.rnumber,GROUP_CONCAT(i.rid) rid FROM room r JOIN room_id i WHERE r.rtid=i.rtid GROUP BY rtid ";
        ArrayList values = new ArrayList();


        return JdbcUtil.queryDataByPage(sql, RoomType.class, currentPgae, pageSize, values.toArray());
    }

}
